package com.lnb.common.utils;

import cn.hutool.crypto.digest.MD5;
import lombok.extern.slf4j.Slf4j;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: lc
 **/
@Slf4j
public class VerificationUtil {


      private static Pattern phone = Pattern.compile("^1[3|4|5|6|7|8|9][0-9]{9}$");
      private static Pattern loginPassword = Pattern.compile("(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[a-zA-Z0-9]{8,16}");
    /**
     * "\^1\(\?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$"
     * 验证手机号码
     * @param mobiles
     * @return
     */
    public static boolean isMobileNO(String mobiles){
        boolean flag = false;
        try{
            Matcher m = phone.matcher(mobiles);
            flag = m.matches();
        }catch(Exception e){
            log.error("请输入正确的手机号");
            flag = false;
        }
        return flag;
    }

    /**
     * 登录密码加密
     * @param password
     * @return
     */
    public static String secretPassword(String password){
        String secretPassword = null;
        try {
            secretPassword = MD5.create().digestHex("");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return secretPassword;
    }


    public static String decryptMD5(String md5) {
        try {
            // 创建MessageDigest对象，指定算法为MD5
            MessageDigest md = MessageDigest.getInstance("MD5");

            // 将要解密的字符串转换为字节数组
            byte[] bytes = md.digest(md5.getBytes());

            // 将字节数组转换为十六进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : bytes) {
                sb.append(String.format("%02x", b));
            }

            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 号码中间四位用*代替
     * @param mobile
     * @return
     */
    public static String  replaceMobile(String mobile){
        try{
            mobile = mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        }catch(Exception e){
            log.error("请输入正确的手机号");
        }
        return mobile;
    }

    public static boolean checkPassword(String password){
        boolean flag = false;
        try{
            Matcher matcher = loginPassword.matcher(password);
            flag = matcher.matches();
        }catch(Exception e){
            log.error("密码必须由8-16位数字和大小写字母组成");
            flag = false;
        }
        return flag;
    }

}
